package com.qymerge.manage.base.controller;

import com.qymerge.manage.base.ro.ProductRO;
import com.qymerge.manage.base.service.IProductService;
import com.qymerge.manage.core.bean.*;
import com.qymerge.manage.core.constant.ParamKey;
import com.qymerge.manage.core.controller.BaseController;
import com.qymerge.manage.core.utils.GsonUtil;
import com.qymerge.manage.core.utils.PrintCodeUtils;
import com.qymerge.manage.core.utils.RequestCheckUtils;
import com.google.common.reflect.TypeToken;
import com.qymerge.manage.core.bean.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *<p>Title: ProductController</p>
 *<p>Description: 产品管理</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/19/0019
 *@Time 17:31
 */
@Controller
@RequestMapping(value = "base/product")
public class ProductManageController extends BaseController {

    @Autowired
    private IProductService productService;

    /**
     * 列表
     *
     * @param parameters
     * @param callback
     */
    @RequestMapping("list")
    public void list(HttpServletRequest request, HttpServletResponse response,

                     @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                     @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<ProductRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<ProductRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        ProductRO ro = paramValue.getData();

        DataTableRO data = productService.list(ro, new PageRequest(common.getPage(), common.getSize()));
        PrintCodeUtils.printSuccess(response, data.build(), callback);
    }

    /**
     * 详情
     *
     * @param request
     * @param response
     * @param callback
     * @throws Exception
     */
    @RequestMapping("detail")
    public void detail(HttpServletRequest request, HttpServletResponse response,

                       @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                       @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {

        ParamValue<ProductRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<ProductRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        ProductRO ro = paramValue.getData();

        RequestCheckUtils.checkNotEmpty(ro.getProductid(), "ID必须");
        ProductRO resultRO = productService.detail(ro.getProductid());

        PrintCodeUtils.printSuccess(response, resultRO, callback);
    }

    /**
     * 增加
     *
     * @param request
     * @param response
     * @param callback
     */
    @RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response,

                     @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                     @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {
        ParamValue<ProductRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<ProductRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        ProductRO ro = paramValue.getData();

        ActionResultRO resultRO = productService.save(ro);
        PrintCodeUtils.print(response, resultRO, callback);
    }

    /**
     * 删除
     *
     * @param parameters
     * @return
     */
    @RequestMapping(value = "/delete")
    public void delete(HttpServletRequest request, HttpServletResponse response,

                       @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                       @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<CommonIdsDTO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<CommonIdsDTO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        CommonIdsDTO commonIdsDTO = paramValue.getData();

        ActionResultRO obj = productService.delete(commonIdsDTO.getIds());
        PrintCodeUtils.print(response, obj, callback);
    }



}
